package com.yingpai.fitness.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.main.util.ToastUtil;
import com.yingpai.fitness.dvp_2_0.main.util.WechatLoginUtil;

/*
* 微信支付的结果回掉
* */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhifu_module_wxapi_result);
        api = WXAPIFactory.createWXAPI(this, null);//此处是公众号id

        api.handleIntent(getIntent(), this);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    WechatLoginUtil.getInstance().wechatCallBack.callBack(resp);
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    ToastUtil.showToast("支付取消");
                    WechatLoginUtil.getInstance().wechatCallBack.callBack(resp);
                    break;
                case BaseResp.ErrCode.ERR_COMM:
                    ToastUtil.showToast("支付被拒绝");
                    WechatLoginUtil.getInstance().wechatCallBack.callBack(resp);
                    break;
                default:
                    ToastUtil.showToast("支付失败");
                    WechatLoginUtil.getInstance().wechatCallBack.callBack(resp);
                    break;
            }
            finish();
        }
    }
}